package aad.storyboard.controller;

import aad.storyboard.R;
import aad.storyboard.view.GraphicView;
import aad.storyboard.view.StoryBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

/**
 * Responsible for the control of the navigation panel that appears onTap, when the GraphicView is tapped in the lower part of the screen 
 */
public class NavigationController{
	
	
	private static boolean isPaused=false;
	private static long lastActionTime=0L;	
	private static GraphicView surface;
	
	private static View controlPanel=null;	
	
	boolean pausing = false;	
	static StoryBar volControl=null;

	static AudioManager audioManager;
	static int maxVolume;
	static int curVolume;
	
	//Singleton
	private static NavigationController inst=new NavigationController();
	
	public static NavigationController getInstance(){
		return inst;
	}
	
	//Singleton
	private NavigationController() {
		
	}
	
	public static NavigationController getInstance(StoryBar vol, Activity a)
	{
		volControl=vol;
		
		a.getWindow().setFormat(PixelFormat.UNKNOWN);		
		audioManager = (AudioManager)a.getSystemService(Context.AUDIO_SERVICE);
	    maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	    curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
	    volControl.setMax(maxVolume);
    	volControl.setProgress(curVolume);
    	
		
		surface=(GraphicView)a.findViewById(R.id.graphicView);				
		
		controlPanel=a.findViewById(R.id.navigation_panel);	
						 	
    	volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

	 @Override
	 public void onStopTrackingTouch(SeekBar arg0) {
		 lastActionTime=SystemClock.elapsedRealtime();
		  }
	
	 @Override
	 public void onStartTrackingTouch(SeekBar arg0) {
		 lastActionTime=SystemClock.elapsedRealtime();
	
	 }

	 @Override
	 public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
	  // TODO Auto-generated method stub
	  audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
	 }
	});
    	surface.postDelayed(onEverySecond, 1000);    	
    	
    	return inst;

	}

	

	/**
	 * set the visibility of the navigation panel to GONE if the screen is not being tapped for the specified period of time
	 *	 */
	private static void clearControl(boolean both) {
		lastActionTime=0;	
		controlPanel.setVisibility(View.GONE);	
	}


	/**
	 *checks, how much time elapsed since the GraphicView has been touched. -> If lastActionTime is bigger than 5 seconds, the navigation control will disappear
	*	 */
	private static Runnable onEverySecond=new Runnable() {
		@Override
		public void run() {
			if (lastActionTime>0 &&
					SystemClock.elapsedRealtime()-lastActionTime>5000) {			
				clearControl(false);
				System.out.println("Running"+lastActionTime);
			}
			if (!isPaused) {
				surface.postDelayed(onEverySecond, 1000);				
			}
		}
	};



	public void handleOnTap(MotionEvent event) {
		if (event.getY()<surface.getHeight()/2) {
			
			System.out.println("OnTap topPanel visble");
		}
		else {
			controlPanel.setVisibility(View.VISIBLE);				
			lastActionTime=SystemClock.elapsedRealtime();
		}		
	}}
